package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.SealTempJurVo;
import com.ruoyi.common.core.domain.entity.Person;
import com.ruoyi.common.core.domain.entity.SealInfo;
import com.ruoyi.common.core.domain.entity.SealJur;
import com.ruoyi.common.core.domain.entity.SealTempjur;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.mapper.SealTempjurMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 个人印章转授回收Service业务层处理
 * 
 * @author ess
 * @date 2021-11-03
 */
@Service
public class SealTempjurBackServiceImpl implements ISealTempjurBackService
{
    @Autowired
    private SealTempjurMapper sealTempjurMapper;

   @Autowired
    private JmService jmService;

    @Autowired
    private IPersonService personService;

    @Autowired
    private ISealInfoService sealInfoService;

    @Autowired
    private ISealJurSignerService sealJurSignerService;

    @Autowired
    private ISealTempjurBackService sealTempjurBackService;

    /**
     * 查询个人印章转授信息
     * 
     * @param tempJurId 印章转授主键
     * @return 印章转授
     */
    @Override
    public SealTempjur selectSealTempjurByTempJurId(String tempJurId)
    {
        return sealTempjurMapper.selectSealTempjurByTempJurId(tempJurId);
    }

    /**
     * 查询个人印章转授列表
     * 
     * @param sealTempjur 印章转授管理
     * @return 印章转授管理
     */
    @Override
    public List<SealTempjur> selectSealTempjurList(SealTempjur sealTempjur) {
        List<SealTempjur> sealTempjurs = new ArrayList<SealTempjur>();
//        处理转授人   参数为转授人姓名

        if (StringUtils.isNotEmpty(sealTempjur.getUseJurPersonId())) {
            List<Person> personList = personService.selectPersonListByPersonName(sealTempjur.getUseJurPersonId());
            if (personList != null && personList.size() > 0) {
                for (Person person : personList) {
                    sealTempjur.setUseJurPersonId(person.getPersonId());
                    List<SealTempjur> sealTempjurQuery = sealTempjurMapper.selectSealTempjurList(sealTempjur);
                    if (sealTempjurQuery != null && sealTempjurQuery.size() > 0) {
                        sealTempjurs.addAll(sealTempjurQuery);
                    }
                }
            }

            return takeSealTempJurReturn(sealTempjurs);
        }

        return takeSealTempJurReturn(sealTempjurMapper.selectSealTempjurList(sealTempjur));

    }

    /**
     * 新增个人印章转授
     * 
     * @param sealTempjur 印章转授管理
     * @return 结果
     */
    @Override
    public int insertSealTempjur(SealTempjur sealTempjur)
    {
//        去除转授次数
        SealJur sealJur = sealJurSignerService.selectSealJurSignerBySealJurId(sealTempjur.getSealJurId());
        if(sealJur.getUseCount()!=null&&sealJur.getUseCount()!=-1){
            if (sealTempjur.getUseCountTemp()!=null&&sealTempjur.getUseCountTemp()!=-1) {
                sealTempjur.setUseCount(sealTempjur.getUseCountTemp());
                sealJur.setUseCount(sealJur.getUseCount()-sealTempjur.getUseCountTemp());
            }
        }


//        原授权数据修改是否已转授字段
        sealJur.setIsAlreadyTempJurSeal(1L);
        sealJurSignerService.updateSealSignerJur(sealJur);


//        主键uuid
        sealTempjur.setTempJurId( UUID.fastUUID().toString().replace("-",""));

        return sealTempjurMapper.insertSealTempjur(sealTempjur);
    }

    /**
     * 修改个人印章转授
     * 
     * @param sealTempjur 印章转授管理
     * @return 结果
     */
    @Override
    public int updateSealTempjur(SealTempjur sealTempjur)
    {
        return sealTempjurMapper.updateSealTempjur(sealTempjur);
    }

    /**
     * 批量删除个人印章转授
     * 
     * @param tempJurIds 需要删除的印章转授主键
     * @return 结果
     */
    @Override
    public int deleteSealTempjurByTempJurIds(String[] tempJurIds)
    {
        //        授权次数归还
        for (String sealTempJurId : tempJurIds) {
            SealTempjur sealTempjur = sealTempjurBackService.selectSealTempjurByTempJurId(sealTempJurId);
            if (sealTempjur.getUseCount()!=null&&sealTempjur.getUseCount()!=-1) {
                SealJur sealJur = sealJurSignerService.selectSealJurSignerBySealJurId(sealTempjur.getSealJurId());
                if (sealJur.getUseCount()!=null&&sealJur.getUseCount()!=-1) {
                    sealJur.setUseCount(sealJur.getUseCount()+sealTempjur.getUseCount());
                    sealJurSignerService.updateSealSignerJur(sealJur);
                }

            }

            sealTempjurMapper.deleteSealTempjurByTempJurId(sealTempJurId);
            //        判断是否还有转授信息     如果没有则把是否已转授修改为  未转授
            List<SealTempjur>  sealTempjurs= sealTempjurMapper.selectSealTempJurBySealJurId(sealTempjur.getSealJurId());
            if (sealTempjurs != null && sealTempjurs.size() > 0) {
//            说明还有转授数据
            } else {
//            是否已转授修改为未转授
                SealJur sealJur = new SealJur();
                sealJur.setSealJurId(sealTempjur.getSealJurId());
                sealJur.setIsAlreadyTempJurSeal(0L);
                sealJurSignerService.updateSealSignerJur(sealJur);
            }
        }



        return 1;
    }

    /**
     * 删除个人印章转授信息
     * 
     * @param tempJurId 印章转授主键
     * @return 结果
     */
    @Override
    public int deleteSealTempjurByTempJurId(String tempJurId)
    {
        SealTempjur sealTempjur = sealTempjurBackService.selectSealTempjurByTempJurId(tempJurId);
        if (sealTempjur.getUseCount()!=null&&sealTempjur.getUseCount()!=-1) {
            SealJur sealJur = sealJurSignerService.selectSealJurSignerBySealJurId(sealTempjur.getSealJurId());
            if (sealJur.getUseCount()!=null&&sealJur.getUseCount()!=-1) {
                sealJur.setUseCount(sealJur.getUseCount()+sealTempjur.getUseCount());
                sealJurSignerService.updateSealSignerJur(sealJur);
            }
        }

        sealTempjurMapper.deleteSealTempjurByTempJurId(tempJurId);
//        判断是否还有转授信息     如果没有则把是否已转授修改为  未转授
      List<SealTempjur>  sealTempjurs= sealTempjurMapper.selectSealTempJurBySealJurId(sealTempjur.getSealJurId());
        if (sealTempjurs != null && sealTempjurs.size() > 0) {
//            说明还有转授数据
        } else {
//            是否已转授修改为未转授
            SealJur sealJur = new SealJur();
            sealJur.setSealJurId(sealTempjur.getSealJurId());
            sealJur.setIsAlreadyTempJurSeal(0L);
            sealJurSignerService.updateSealSignerJur(sealJur);
        }

        return 1;
    }

    @Override
    public boolean checkAreadyTempJur(SealTempjur sealTempjur) {
        try {
          SealTempjur sealTempjurAready=  sealTempjurMapper.selectSealTempJurBySealIdAndUsePersonId(sealTempjur.getSealJurId(),sealTempjur.getUseJurPersonId());
            if (sealTempjurAready != null) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }


    private List<SealTempjur>  takeSealTempJurReturn(List<SealTempjur> sealTempjurs){
        if (sealTempjurs!=null&&sealTempjurs.size()>0) {
            for (SealTempjur sealTempjur : sealTempjurs) {
                SealTempJurVo sealTempJurVo = new SealTempJurVo();
                //            人员姓名   生日后四位    编号
                Person person = personService.selectPersonByPersonId(sealTempjur.getUseJurPersonId());
                if (person!=null) {
                    sealTempJurVo.setPersonNum(person.getEmpNum());
                    sealTempJurVo.setPersonName(person.getPersonName());
                    sealTempJurVo.setBirNumFour(person.getBirNum().substring(4));
                }

                //            印章名称
                SealJur sealJur = sealJurSignerService.selectSealJurSignerBySealJurId(sealTempjur.getSealJurId());
                if (sealJur != null) {
                    SealInfo sealInfo = sealInfoService.selectSealInfoBySealInfoId(sealJur.getSealId());
                    if (sealInfo != null) {
                        sealTempJurVo.setSealName(sealInfo.getSealName());
                    } else {
                        sealTempJurVo.setSealName(Constants.ERR_NAME);
                    }
                } else {
                    sealTempJurVo.setSealName(Constants.ERR_NAME);
                }
                sealTempjur.setSealTempJurVo(sealTempJurVo);
            }

        }

        return sealTempjurs;
    };
}
